using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using SubversionReports.Utilities;

namespace SubversionReports.Web.Controls
{
    public partial class RepositoryLogin : UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                OpenSourceBlurb.Visible = Request.Url.Host.ToLower().Contains("subersionreports.org");
                if(Request["RepositoryUrl"] != null)
                {
                    GoToRepositoryOrError(Request["RepositoryUrl"], string.Empty, string.Empty);
                }
                else
                {
                    RepositoryRepeater.DataSource = SvnWebDav.GetRepositoryList();
                    RepositoryRepeater.DataBind();
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the RepositoryLink control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void RepositoryLink_Click(object sender, EventArgs e)
        {
            LinkButton repoLink = sender as LinkButton;
            if (repoLink != null)
            {
                if (SvnWebDav.AnonymousLoginTested(repoLink.Text))
                    GoToRepositoryOrError(repoLink.Text, string.Empty, string.Empty);
                else
                    RepositoryUrl.Text = repoLink.Text;
            }
        }

        /// <summary>
        /// Handles the Click event of the LoginButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LoginButton_Click(object sender, EventArgs e)
        {
            GoToRepositoryOrError(RepositoryUrl.Text, RepositoryUsername.Text, RepositoryPassword.Text);
        }


        /// <summary>
        /// Goes to repository or errors.
        /// </summary>
        /// <param name="repositoryUrl">The repository URL.</param>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        private void GoToRepositoryOrError(string repositoryUrl, string username, string password)
        {
            try
            {
                if (!repositoryUrl.EndsWith("/"))
                    repositoryUrl += "/";
                SvnWebDav.AddRepositoryCredential(repositoryUrl, username, password);
            }
            catch (Exception ex)
            {
                ErrorLabel.Text = string.Format("Login failed at {0} for user {1}", repositoryUrl, username);
                ExceptionMessage.Text = ex.Message;
                ErrorContainer.Visible = true;
                RepositoryUrl.Text = repositoryUrl;
            }

            if (SvnWebDav.AuthenticationSuceeded)
                Response.Redirect(string.Format("{0}", Request.RawUrl.Replace("RepositoryUrl=", "RU=")), true);
        }
    }
}